package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Module;
import com.sjlh.centurylianhua.mapper.ModuleMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/module")
@CrossOrigin
public class ModuleController {
    @Resource
    private ModuleMapper moduleMapper;
    /**
     * 分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        QueryWrapper<Module> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isnotNull(searchCon.getName()), "moduleName", searchCon.getName());
        // 封装分页对象
        Page<Module> page = new Page<Module>(searchCon.getPage(), searchCon.getLimit());
        Page<Module> modulePage = moduleMapper.selectPage(page, wrapper);
        List<Module> list=modulePage.getRecords();//所有数据
        long count=modulePage.getTotal();//总条数
        ResultData rs=new ResultData(true,20000,"成功",list,count);
        return rs;
    }
    /**
     *根据角色名字精确查询角色
     * */
    public Module getOne(@RequestBody Module module){
        QueryWrapper<Module> wrapper = new QueryWrapper<>();
        wrapper.eq( "moduleName", module.getModuleName());
        return moduleMapper.selectOne(wrapper);
    }

    /**
     *
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody Module module){
        Module m = this.getOne(module);
        if (m == null) {
            int r=moduleMapper.insert(module);
            if(r==1)
                return new ResultData(true,20000,"添加角色成功",true,null);
            else
                return new ResultData(false,20000,"添加角色失败",false,null);
        } else {
            return new ResultData(false,20001,"角色名已存在",false,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody Module module){
        Module m = this.getOne(module);
        if (m == null) {
            int r = moduleMapper.updateById(module);
            if(r==1)
                return new ResultData(true,20000,"修改角色成功",true,null);
            else
                return new ResultData(false,20000,"修改角色失败",false,null);
        } else {
            return new ResultData(false,20001,"修改过的角色名已存在",null,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/del")
    public Object del(@RequestBody Module module){
        int rs = moduleMapper.deleteById(module.getId());

        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }
}
